package com.guli.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.educms.bean.CrmBanner;
import com.guli.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-10
 */
@RestController
@RequestMapping("/educms/banner")
@CrossOrigin
public class BannerAdminController {

    @Resource
    private CrmBannerService crmBannerService;

    @ApiOperation("分页显示")
    @GetMapping("bannerPage/{current}/{limit}")
    public R bannerPage(@PathVariable("current") long current,@PathVariable("limit")long limit){
        Page<CrmBanner> bannerPage = new Page<>(current,limit);
        crmBannerService.page(bannerPage, null);
        long total = bannerPage.getTotal(); //总记录数
        List<CrmBanner> records = bannerPage.getRecords(); // 数据list集合
        Map<Object, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("getBannerById/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("item", banner);
    }
    @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    public R save(@RequestBody CrmBanner banner) {
        crmBannerService.save(banner);
        return R.ok();
    }
    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateById(banner);
        return R.ok();
    }
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }
}

